package com.example.moretherz.demoa3_ds0926;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class Myadpter extends BaseAdapter {
    private ArrayList<Student> studentArrayList;
    private Context context;
    private IOnDelListener dellistener;
    private IOnEdtListener editlistener;



    public Myadpter (Context context,ArrayList studentArrayList,IOnDelListener dellistener,IOnEdtListener editlistener){
        this.context=context;
        this.studentArrayList=studentArrayList;
        this.dellistener = dellistener;
        this.editlistener = editlistener;
    }
    public void changeData(ArrayList<Student> studentArrayList){
        this.studentArrayList=studentArrayList;
        notifyDataSetInvalidated();

    }

    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return studentArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    int count  = 0;
    public View getView(final int i, View view, ViewGroup viewGroup) {
             Log.e("Myadpter", "getview " + i + " count = " + count);
             if(view == null){
                 count++;
                 LayoutInflater layoutInflater = LayoutInflater.from(context);
                 view = layoutInflater.inflate(R.layout.my_list_item,viewGroup,false);

                 StuViewHolder holder = new StuViewHolder();
                 holder. headImg = view.findViewById(R.id.headView);
                 holder. nameView = view.findViewById(R.id.NameView);
                 holder.  ageView = view.findViewById(R.id.ageView);
                 holder  .del  = view.findViewById(R.id.delete_btn);
                 holder.edit = view.findViewById(R.id.edit_btn);
                 view.setTag(holder);
             }

        Student stu = studentArrayList.get(i);

        StuViewHolder holder = (StuViewHolder) view.getTag();

        holder.headImg.setImageResource(stu.getImgID());
        holder.nameView.setText(stu.getStudentName());
        holder.ageView.setText(String.valueOf(stu.getAge()));
        holder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dellistener.del(i);
            }
        });
        holder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editlistener.edit(i);
            }
        });
        return view;
    }
}
